home *** CD-ROM | disk | FTP | other *** search
- Path: news.clark.net!usenet
- From: yom@clark.net (yom)
- Newsgroups: comp.lang.c
- Subject: Re: more problems with qsort
- Date: 3 Mar 1996 05:19:51 GMT
- Organization: home
- Message-ID: <4hba5n$2ga@clarknet.clark.net>
- References: <177399702S86.JW1675A@american.edu> <4h0j9e$ng5@clarknet.clark.net> <4h81m0$avr@solutions.solon.com>
- NNTP-Posting-Host: yom.clark.net
- Mime-Version: 1.0
- Content-Type: Text/Plain; charset=US-ASCII
- X-Newsreader: WinVN 0.99.7
-
- In article <4h81m0$avr@solutions.solon.com>, seebs@solutions.solon.com
- says...
- >
- >In article <4h0j9e$ng5@clarknet.clark.net>, yom <yom@clark.net> wrote:
- >>Since you're trying to sort a char**, the qsort function call must
- >>look like this:
- >
- >>qsort(array,lines,sizeof(char **),(int (*)(void *,void *)) compare);
- >
- >This is entirely wrong. If you're trying to sort an array of pointers
- >to char (or a char ** acting like an array of char *'s), you must use
- > qsort(ary, nelem, sizeof(array[0]), compare);
- >... and array[0] will be a char *, which may be different from a
- >char **.
-
- Oops. You're correct. Thanks for correcting the mistake.
-
- >
- >>And your compare function must be defined like this:
- >
- >>int compare(char **a,char **b) {return strcmp(*a,*b);}
- >
- >No, it really mustn't. It must be defined like this:
- >int compare(const void *a, const void *b) {
- > return strcmp(*(char **) a, *(char **) b);
- >}
-
- Well I disagree on this one. Results of 1) defining the compare
- function that takes two char** arguments and casting the function call
- to qsort and 2) defining the compare function that takes two void*
- arguments and casting the arguments to char ** in strcmp call are
- identical. Also, I tend to not use "const" because some compilers do
- not support it.
-
- Song (yom@clark.net)
-
-